package top.itaso.lib.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

/**
 * @Description: 下载进度条/上传进度条 等
 * @Author itsdf07
 * @E-Mail 923255742@qq.com
 * @Github https://github.com/itsdf07
 * @Date 2021/12/07
 */
public class DownloadProgressBarView2 extends ConstraintLayout {
    /**
     * 进度条
     */
    private ProgressBar mUploadProgressBar;
    /**
     * 文件信息
     */
    private TextView mTaskFile;
    /**
     * 进度值
     */
    private TextView mTaskProgress;
    private ImageView mClose;

    private ICloseCallback closeCallback;

    public interface ICloseCallback {
        /**
         * 上传中断
         */
        void onInterrupt();
    }

    public void setCloseCallback(ICloseCallback closeCallback) {
        this.closeCallback = closeCallback;
    }

    public DownloadProgressBarView2(@NonNull Context context) {
        this(context, null);
    }

    public DownloadProgressBarView2(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DownloadProgressBarView2(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        inflate(getContext(), R.layout.layout_download_progressview2, this);
        mUploadProgressBar = findViewById(R.id.pb_upload_progressbar);
        mTaskFile = findViewById(R.id.task_file);
        mTaskProgress = findViewById(R.id.task_progress);
        mClose = findViewById(R.id.upload_interrupt);
        mClose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (closeCallback != null) {
                    closeCallback.onInterrupt();
                }
            }
        });
    }

    public void refreshProgress(String filePath, int progress) {
        mUploadProgressBar.setProgress(progress);
        mTaskFile.setText(filePath);
        mTaskProgress.setText(mUploadProgressBar.getProgress() + "%");
    }

    public int curProgress() {
        return mUploadProgressBar.getProgress();
    }
}
